package com.douziit.meilianoa.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.douziit.meilianoa.base.constant.HeaderNameConstant;
import com.douziit.meilianoa.base.enums.ResponseCode;
import com.douziit.meilianoa.base.enums.RoleEnum;
import com.douziit.meilianoa.base.exception.TipException;
import com.douziit.meilianoa.base.redis.UserTokenBean;
import com.douziit.meilianoa.base.redis.UserTokenService;
import com.douziit.meilianoa.base.request.LoginRequest;
import com.douziit.meilianoa.base.response.Account.AccountResponse;
import com.douziit.meilianoa.base.utils.JacksonUtil;
import com.douziit.meilianoa.base.utils.StrEncryptUtil;
import com.douziit.meilianoa.base.utils.config.JsonMap;
import com.douziit.meilianoa.entity.Account;
import com.douziit.meilianoa.mapper.AccountMapper;
import com.douziit.meilianoa.service.AccountService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户操作    服务实现类
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper,Account> implements AccountService {

    @Autowired
    private AccountService accountService;

    @Autowired
    private UserTokenService tokenService;

    @Autowired
    private HttpServletRequest request;

    @Override
    public AccountResponse login(LoginRequest req) {
        String pwd = StrEncryptUtil.md5(req.getPassword());
        Account account=accountService.getOne(new QueryWrapper<Account>()
                        .eq("phone",req.getPhone()));
        //判断用户是否存在
        if (null ==account ) {
            throw new TipException(ResponseCode.USER_NOT_EXIST);
        }
        //判断密码是否正确
        if (!pwd.equals(account.getPassword())) {
            throw new TipException(ResponseCode.PASSWORD_ERR);
        }
        //判断是否禁用
        if (RoleEnum.UserStatus.DISABLE.getVal().equals(account.getStatus())) {
            throw new TipException(ResponseCode.USER_DISABLE);
        }

        AccountResponse response = new AccountResponse();

        BeanUtils.copyProperties(account, response);
        //生成token
        String token = tokenService.generate(new UserTokenBean(account.getId()), account);
        response.setToken(token);
        return response;
    }

    @Override
    public void loginOut() {
        String token = request.getHeader(HeaderNameConstant.API_USER_TOKEN);
        tokenService.deleteUserToken(token);
    }

    @Override
    public Account findUserInfoByToken() {
        // 获取用户Token
        String userTokenStr = request.getHeader(HeaderNameConstant.API_USER_TOKEN);

        // 从redis中查询用信息
        JsonMap dataMap = tokenService.findUserInfo(userTokenStr);
        System.out.println(JSON.toJSONString(dataMap));
        return JacksonUtil.map2pojo(dataMap, AccountResponse.class);
    }

    @Override
    public AccountResponse loginPc(LoginRequest req) {
        String pwd = StrEncryptUtil.md5(req.getPassword());
        Account account=accountService.getOne(new QueryWrapper<Account>()
                .eq("phone",req.getPhone()));
        //判断用户是否存在
        if (null ==account ) {
            throw new TipException(ResponseCode.USER_NOT_EXIST);
        }
        //判断密码是否正确
        if (!pwd.equals(account.getPassword())) {
            throw new TipException(ResponseCode.PASSWORD_ERR);
        }
        //判断是否禁用
        if (RoleEnum.UserStatus.DISABLE.getVal().equals(account.getStatus())) {
            throw new TipException(ResponseCode.USER_DISABLE);
        }
        //pc端判断是否为超管(只有超管有权限登入后台)
        if(RoleEnum.SUPER_ADMIN.getVal().equals(account.getRole())){
            throw new TipException(ResponseCode.LOGIN_AUTH);
        }

        AccountResponse response = new AccountResponse();

        BeanUtils.copyProperties(account, response);
        //生成token
        String token = tokenService.generate(new UserTokenBean(account.getId()), account);
        response.setToken(token);
        return response;
    }
}
